#include <bits/stdc++.h>

#define fi first
#define se second
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

const int N = 3e6 + 10;

template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

using vecc = vector < char >;

int n;
int t[N], f[N];
vector < char > pot;

char get() {
	char ch = getchar();
	while(!isdigit(ch) && ch != 'x') ch = getchar();
	return ch;
}

string trans1(char x) {
	string ret = "s((";
	ret += x;
	ret += "))";
	return ret;
}
/*
string trans1(vecc t) {
	string ret;
	for(auto v : t)
		if(ret.size()) ret += "*" + trans1(v);
		else ret += trans1(v);
	ret = "(" + ret + ")";
	return ret;
}
*/
void cont(string &x, char y) {
	//int l = x.size();
	if(x == "") x += y;
	else x += "+", x += y;
	//cerr <<"!" <<  l << " " << x.size() << endl;
}

void cont(string &x, string y) {
	if(x == "") x += y;
	else x += "+", x += y;
}

void contmul(string &x, string y) {
	if(x == "") x += y;
	else x += "*" + y;
}

char gchar() { char t = pot.back(); pot.pop_back(); return t; }

string get1() {
	string t = trans1(gchar());
	return t;
}

string get2() {
	string t = trans1(gchar());
	t = "(" + t + "+" + trans1(gchar()) + ")";
	return t;
}

string getif(int x) {
	char t;
	string cur = "";
	while((t = gchar()) != 'x' || cur == "") {
		contmul(cur, trans1(t));
	}
	cur = cur + "-";
	cur += "s(s(s(";
	cur += t;
	rep(i, 1, x) cur = cur + "-" + get1();
	cur = cur + ")))";
	//cerr << "GETIFXXXXX!" << " " << x << " : " << cur << endl;
	return cur;
}

int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
	freopen("1.out", "w", stdout);
#endif
	scanf("%d", &n); rep(i, 1, n) { pot.eb(get()); }
	int m; scanf("%d", &m); rep(i, 1, m) scanf("%d %d", &t[i], &f[i]);
	string ans = ""; vecc tmp;
	reverse(pot.begin(), pot.end()); // reverse
	ans += trans1(gchar());
	ans += "-" + trans1(gchar());
	rep(i, 0, 30) {
		ans = "(" + ans + ")*" + get2();
		string curif = "";
		rep(j, 1, m) if(f[j] >> (30 - i) & 1) cont(curif, getif(t[j]));
		//cerr << "GETIF" << curif << endl;
		
		if(curif.size()) ans = ans + "+" + curif;

		//cerr << curif << endl;
		//if(curif.size()) ans = ans + "+GETIF!";
		
		//cerr << ans << endl;
		//break;
	}
	ans = "(" + ans + ")";
	cerr << pot.size() << " " << ans.size() << endl;
	//while(pot.size()) ans += "*" + get1();
	string ret = "";
	while(pot.size()) {
		char t = gchar();
		cont(ret, t);
		//cerr << t << " " << ret.size() << endl;
	}
	cerr << "!" << ret.size() << endl;
	if(ret.size()) {
		rep(i, 1, 10) ret = "s(" + ret + ")";
		ans = ans + "*" + ret;
	}
	cerr << ans.size() << " " << pot.size() << endl;
	cout << ans << endl;
}
